#ifndef	__PID_H
#define	__PID_H


/***************************************************************************/ 
  
typedef struct  
{ 
    signed long int SetPoint;                // 设定目标Desired value 
		signed long int RealPoint;               // 当前值 Present value                               
	  float Proportion;                   			// 比例常数Proportional Const 
	  float Integral;														// 积分常数Integral Const
		float Proportion_BU; 											// 比例常数备份 
	  signed long int Derivative;              // 微分常数Derivative Const                      
	  signed long int LastError;               // Error[-1]                    
	  signed long int PrevError;               // Error[-2]                    
	  signed long int SumError;                // Sums of Errors 
	
	  float Integral_cycle;  		   //积分周期调整系数    unsigned char  
    signed long int SumError_max; 
	  signed long int RealPoint_max; 
    signed long int N_Error_max1; 
	  signed long int N_Error_max2; 
	  unsigned int    Tp;//振荡周期
	  unsigned char   FirstCycleFlag;  		  //积分周期调整系数 
    unsigned char   T_Adj;
  	unsigned char   Ki_adj; 
} PID; 


typedef struct  
{     signed long int SetPoint;                // 设定目标Desired value 
      signed long int RealPoint;               // 当前值 Present value                              
	  float Proportion;                        // 比例常数Proportional Const            
	  float Integral;						   // 积分常数Integral Const
	  float Derivative;              // 微分常数Derivative Const                      
	  signed long int LastError;               // Error[-1]                    
	  signed long int PrevError;               // Error[-2] 
	  signed long int BePrevError;            // Error[-3]                   
	  signed long int SumError;                // Sums of Errors 	                     				  
} PID2;


typedef struct  
{    
	    unsigned short int Temp_buf[150];
	    unsigned short int Temp_max;
	    unsigned short int Temp_min;
    	signed long int SetPoint;                // 设定目标Desired value 
      signed long int RealPoint;               // 当前值 Present value             
	    float Proportion;                
	    float Integral;
	    signed long int Derivative;              // 微分常数Derivative Const                      
	    signed long int LastError;               // Error[-1]                    
	    signed long int PrevError;               // Error[-2]                    
	    signed long int SumError;                // Sums of Errors 
	    unsigned char  Integral_cycle;  		   //积分周期调整系数   
	    unsigned char  step;  		   //整定步骤
	    unsigned char  Pwr_buf[60];  //功率记录
	    unsigned char  PwrCnt;  //功率记录
} PID_TUN; 


typedef struct  
{    
	     unsigned char  step;  //测试过程
       unsigned char start_pwr;
	     unsigned char test_pwr;
	     unsigned short int temp[200];
	     unsigned short int temp_first;
       unsigned short int delay;
	     unsigned short int cnt;
	     unsigned short int cycle;
	     unsigned short int rate;
	     unsigned short int record_cnt;
	     unsigned char  Tu;
	     unsigned char  Ta;
	     unsigned char  Ti;
	     unsigned char  Td;
	
} CYCLE_PARA; 

signed long int   PIDarithmetic_tuning(PID_TUN *pp);
signed long int   PIDarithmetic_Out(PID *pp);
signed long int   PIDarithmetic_In(PID *pp);
signed long int   PID_moter(PID2 *pp);

#endif

